package tt.dz.egobus.servicemanager.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;
import tt.dz.egobus.api.commoncenter.bean.ConfigBack;
import tt.dz.egobus.api.commoncenter.bean.ConfigTypeBack;
import tt.dz.egobus.api.managerservice.busservice.BusServiceVo;
import tt.dz.egobus.api.managerservice.manager.*;
import tt.dz.egobus.api.managerservice.relative.DriverRelativeView;
import tt.dz.egobus.api.usercenter.staff.StaffBack;
import tt.dz.egobus.bean.BusinessStatus;
import tt.dz.egobus.bean.comment.TagBack;
import tt.dz.egobus.servicemanager.business.convert.BusinessRemarkConverter;
import tt.dz.egobus.servicemanager.business.key.BusinessCache;
import tt.dz.egobus.servicemanager.business.point.AddressPoint;
import tt.dz.egobus.servicemanager.entities.*;
import tt.dz.egobus.servicemanager.vo.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.apache.commons.lang3.StringUtils.defaultString;
import static tt.dz.egobus.api.util.ResponseHelper.copy;

/**
 * @author Anson
 * @date 17-3-30
 * @name pojo, vo转换的工具类
 * @since 1.0.3
 */
public final class PojoConvertVoUtils {

    private static final Converter<BusService, String> SERVICE_REMARK_CONVERTER = new BusinessRemarkConverter();

    /**
     * @param info 订单详细信息
     * @author Anson
     * @date 17-3-29
     * @name 转换封装包，转换businessVO
     * @since 1.0.3
     */
    public static final BusinessVo serviceInfoConvertBusinessVo(BusinessServiceInfo info) {
        BusinessVo vo = new BusinessVo();
        BusinessService service = info.getBusinessService();
        if (service != null) {
            vo.setId(service.getId());
            vo.setBusinessId(service.getBusinessId());
            vo.setUserId(service.getUserId());
            vo.setServiceRegisterId(service.getServiceRegisterId());
            vo.setDriverId(service.getDriverId());
            vo.setStatus(service.getStatus());
            vo.setStatusMsg(BusinessStatus.getStatusMsg(service.getStatus()));
            vo.setBusinessCode(service.getBusinessCode());
            vo.setServiceFee(service.getServiceFee());
            vo.setRequestType(service.getRequestType());
        }

        vo.setBusId(info.getBusId());
        vo.setDistance(info.getDistance());
        vo.setAcceptX(info.getAcceptX());
        vo.setAcceptY(info.getAcceptY());
        vo.setStartX(info.getStartX());
        vo.setStartY(info.getStartY());
        vo.setStartAddress(info.getStartAddress());
        vo.setEndX(info.getEndX());
        vo.setEndY(info.getEndY());
        vo.setEndAddress(info.getEndAddress());
        vo.setValidate(info.isValidate());
        vo.setCreateTime(info.getCreateTime());
        vo.setEndTime(info.getEndTime());
        //用户请求的起始地理位置
        //用户请求开始地理位置
        String addressStart = info.getRequestStartAddress();
        double xStart = info.getRequestStartX();
        double yStart = info.getRequestStartY();

        //用户请求达到地理位置
        String addressEnd = info.getRequestEndAddress();
        double xEnd = info.getRequestEndX();
        double yEnd = info.getRequestEndY();

        vo.setAddressStart(addressStart);
        vo.setxStart(xStart);
        vo.setyStart(yStart);

        vo.setAddressEnd(addressEnd);
        vo.setxEnd(xEnd);
        vo.setyEnd(yEnd);

        vo.setUserName(info.getUserName());
        vo.setUserPhone(info.getUserPhone());
        vo.setPassengerName(info.getPassengerName());
        vo.setPassengerPhone(info.getPassengerPhone());
        Date bespeakTime = info.getBespeakTime();
        if (bespeakTime != null) {
            vo.setBespeakTime(bespeakTime.getTime());
        }
        vo.setIsRideSelf(vo.getIsRideSelf());


        BusServiceViewVo busServiceViewVo = info.convertBusService();
        if (busServiceViewVo != null) {
            BusServiceVo busServiceVo = new BusServiceVo();
            busServiceVo.setActive(busServiceViewVo.getIsActive());
            busServiceVo.setBasicsCost(busServiceViewVo.getBasicsCost());
            busServiceVo.setBusinessCode(busServiceViewVo.getBusinessCode());
            busServiceVo.setMilesLimit(busServiceViewVo.getMilesLimit());
            busServiceVo.setId(busServiceVo.getId());
            busServiceVo.setName(busServiceViewVo.getName());
            busServiceVo.setPriceUnit(busServiceViewVo.getPriceUnit());
            busServiceVo.setTimeUnit(busServiceViewVo.getTimeUnit());
            busServiceVo.setTimeLimit(busServiceViewVo.getTimeLimit());
            vo.setBusServiceVo(busServiceVo);
        }


        return vo;
    }

    /**
     * @param request
     * @author Anson
     * @date 17-3-30
     * @name 转换异常业务请求为系统pojo
     * @since 1.0.3
     */
    public static final BusinessExceptionAlert exceptionRequestConvertExceptionAlert(ExceptionRequest request) {
        BusinessExceptionAlert alert = new BusinessExceptionAlert();

        /**
         * 上报地点经度坐标
         */
        alert.setX(request.getX());
        /**
         * 上报地点纬度坐标
         */
        alert.setY(request.getY());
        /**
         * 上报的地理位置名称
         */
        alert.setAddress(request.getAddress());
        /**
         * 异常信息
         */
        alert.setException(request.getException());
        /**
         * 异常类型 1:系统 2:APP 3:车辆 4:其他
         */
        alert.setExceptionType(request.getExceptionType());

        return alert;
    }

    public static final AddressPoint driverDoTaskRequestConvetAddressPoint(DriverDoTaskRequest request) {
        AddressPoint addressPoint = new AddressPoint();
        addressPoint.setY(request.getY());
        addressPoint.setX(request.getX());
        addressPoint.setAddress(request.getAddress());
        return addressPoint;
    }

    public static final BusinessFinishVo businessDetailConvertBusinessFinishVo(BusinessDetail businessDetail) {
        BusinessFinishVo vo = new BusinessFinishVo();
        vo.setDriverId(businessDetail.getDriverId());
        vo.setUserId(businessDetail.getUserId());
        vo.setUserMobile(businessDetail.getUserMobile());
        vo.setBusinessId(businessDetail.getBusinessId());
        vo.setX(businessDetail.getX());
        vo.setY(businessDetail.getY());
        vo.setAddress(businessDetail.getAddress());
        vo.setDistance(businessDetail.getDistance());
        vo.setDriverAccDistance(businessDetail.getDriverAccDistance());
        vo.setEndTime(businessDetail.getEndTime());
        return vo;
    }

    public static final BusinessCodeServiceStatus busServiceConvertBusinessCodeServiceStatus(BusService service) {
        BusinessCodeServiceStatus serviceStatus = new BusinessCodeServiceStatus();
        if (service == null) {
            return serviceStatus;
        }
        serviceStatus.setName(service.getName());
        serviceStatus.setBusinessCode(service.getBusinessCode());
        serviceStatus.setActive(service.isActive());
        serviceStatus.setId(service.getId());
        serviceStatus.setBasicsCost(service.getBasicsCost());
        serviceStatus.setMilesLimit(service.getMilesLimit());
        serviceStatus.setTimeLimit(service.getTimeLimit());
        serviceStatus.setPriceUnit(service.getPriceUnit());
        serviceStatus.setTimeUnit(service.getTimeUnit());
        String serviceRemark = service.getServiceRemark();
        if (StringUtils.isEmpty(serviceRemark)) {
            //当前服务说明为空的时候,则采用默认的服务说明
            serviceRemark = SERVICE_REMARK_CONVERTER.convert(service);
        }
        serviceStatus.setServiceRemark(serviceRemark);
        return serviceStatus;
    }


    /**
     * @param businessService
     * @param businessServiceInfo
     * @author Anson
     * @date 17-4-1
     * @name 转换派单信息
     * @since 1.0.3
     */
    public static final TaskInfo businessDetailConvertTaskInfo(BusinessService businessService,
                                                               BusinessServiceInfo businessServiceInfo) {
        String userId = businessService.getUserId();
        String driverId = businessService.getDriverId();
        Date startTime = businessServiceInfo.getCreateTime();
        String busId = businessServiceInfo.getBusId();
        int status = businessService.getStatus();
        int businessCode = businessService.getBusinessCode();
        String businessId = businessService.getBusinessId();

        //用户请求开始地理位置
        String address = businessServiceInfo.getRequestStartAddress();
        double x = businessServiceInfo.getRequestStartX();
        double y = businessServiceInfo.getRequestStartY();

        //用户请求达到地理位置
        String addressEnd = businessServiceInfo.getRequestEndAddress();
        double xEnd = businessServiceInfo.getRequestEndX();
        double yEnd = businessServiceInfo.getRequestEndY();

        TaskInfo info = new TaskInfo();
        info.setBusinessId(businessId);
        info.setBusinessCode(businessCode);
        info.setUserId(userId);
        info.setBusId(busId);
        info.setX(x);
        info.setY(y);
        info.setStartTime(startTime);
        info.setUserId(userId);
        info.setDriverId(driverId);
        info.setAddress(address);
        info.setStatus(status);
        info.setAddressEnd(addressEnd);
        info.setxEnd(xEnd);
        info.setyEnd(yEnd);
        return info;
    }

    /**
     * @param list 司机集合
     * @author Anson
     * @date 17-4-18
     * @name 根据司机返回结果，转换成view集合
     * @since 1.0.4
     */
    public static List<DriverViewVo> staffBackListConvertDriverViewVos(List<StaffBack> list) {
        if (list == null) {
            return new ArrayList<DriverViewVo>();
        }
        List<DriverViewVo> datas = new ArrayList<DriverViewVo>();
        DriverViewVo v = null;
        for (StaffBack vo : list) {
            v = new DriverViewVo();
            datas.add(v.view(vo));
        }
        return datas;
    }


    /**
     * @param list 模块集合
     * @author Anson
     * @date 17-8-11
     * @name 根据模块返回结果，转换成view集合
     * @since 1.0.6
     */
    public static List<ModuleViewVo> convertModuleViewVoList(List<ConfigTypeBack> list) {
        if (list == null) {
            return new ArrayList<ModuleViewVo>();
        }
        List<ModuleViewVo> datas = new ArrayList<ModuleViewVo>();
        ModuleViewVo v = null;
        for (ConfigTypeBack vo : list) {
            v = new ModuleViewVo();
            datas.add(v.view(vo));
        }
        return datas;
    }

    /**
     * @param list 模块集合
     * @author Anson
     * @date 17-8-11
     * @name 根据配置返回结果，转换成view集合
     * @since 1.0.6
     */
    public static List<RuntimeConfigViewVo> convertRuntimeConfig(List<ConfigBack> list) {
        if (list == null) {
            return new ArrayList<RuntimeConfigViewVo>();
        }
        List<RuntimeConfigViewVo> datas = new ArrayList<RuntimeConfigViewVo>();
        RuntimeConfigViewVo v = null;
        for (ConfigBack vo : list) {
            v = new RuntimeConfigViewVo();
            datas.add(v.view(vo));
        }
        return datas;
    }

    /**
     * @param list 标签结果集
     * @author Anson
     * @date 17-8-14
     * @name 转换当前订单标签管理节点返回结果转换view试图
     * @since 1.0.6
     */
    public static List<OrderTagViewVo> convertTagBackToViews(List<TagBack> list) {

        if (list == null) {
            return new ArrayList<OrderTagViewVo>();
        }
        List<OrderTagViewVo> datas = new ArrayList<OrderTagViewVo>();
        OrderTagViewVo v = null;
        for (TagBack vo : list) {
            v = new OrderTagViewVo();
            datas.add(v.view(vo));
        }
        return datas;
    }

    /**
     * @param businessCache 业务缓存
     * @author Anson
     * @date 17-8-15
     * @name 业务缓存转换为分发放信息
     * @since 1.0.6
     */
    public static DistributeDriverTask businessCacheConvertDistributeDriverTask(
            BusinessCache businessCache) {
        DistributeDriverTask distributeDriverTask = new DistributeDriverTask();
        distributeDriverTask.setBusinessId(businessCache.getBusinessId());
        distributeDriverTask.setUserId(businessCache.getUserId());
        distributeDriverTask.setGrabLimitTimeoutSeconds(businessCache.getGrabLimitTimeoutSeconds());
        distributeDriverTask.setLimitTimeoutSeconds(businessCache.getGrabLimitTimeoutSeconds());
        distributeDriverTask.setBusinessCreateTime(businessCache.getCreateDate());
        distributeDriverTask.setPointVos(copy(businessCache.getDrivers(), BusPointVo.class));
        return distributeDriverTask;
    }

    /**
     * @param relative
     * @author Anson
     * @date 17-8-25
     * @name 转换为车辆关系试图
     * @since 1.0.6
     */
    public static DriverRelativeView driverRelativeConvertRelativeView(DriverRegisterBusRelative relative) {
        if (relative == null) {
            return null;
        }
        DriverRelativeView view = new DriverRelativeView();
        Bus bus = relative.getBus();
        view.setBusId(relative.getBusId());
        if (bus != null) {
            view.setBusNo(bus.getCarNo());
            BusType busType = bus.getBusType();
            if (busType != null) {
                view.setBusSeatNum(busType.getSeatNum());
            }
        }
        view.setDriverId(relative.getDriverId());
        view.setDriverName(relative.getDriverName());
        view.setPhoneNumber(relative.getPhoneNumber());
        view.setStatus(relative.getStatus());
        return view;
    }

    /**
     * @param info
     * @param view
     * @author Anson
     * @date 17-8-29
     * @name 订单详细表转换视图
     * @since 1.0.7
     */
    public static void convertBusinessInfoToView(BusinessServiceInfo info, BusinessView view) {
        //封装业务公共信息
        packageServiceView(info.getBusinessService(), view);
        //封装当前服务细节视图信息
        packageServiceInfoView(info, view);
    }

    /**
     * @param info 服务信息详细信息
     * @param view 当前视图信息
     * @author Anson
     * @date 17-5-16
     * @name 封装当前服务细节视图信息
     * @since 1.0.4
     */
    public static void packageServiceInfoView(BusinessServiceInfo info, BusinessView view) {
        if (info == null || view == null) {
            return;
        }
        /**
         * 当前行程数单位M/米
         * [结束时候,存在有意义值]
         */
        double distance = info.getDistance();

        /**
         * 接单起点经度
         * [不一定存在有效值,要根据实际业务在状态情况]
         */
        double acceptX = info.getAcceptX();

        /**
         * 接单起点纬度
         * [不一定存在有效值,要根据实际业务在状态情况]
         */
        double acceptY = info.getAcceptY();

        /**
         * 行程起点经度
         * [不一定存在有效值,要根据实际业务在状态情况]
         */
        double startX = info.getStartX();

        /**
         * 行程起点纬度
         */
        double startY = info.getStartY();

        /**
         * 行程开始地点
         */
        String startAddress = defaultString(info.getStartAddress(), "");

        /**
         * 行程结束经度
         */
        double endX = info.getEndX();

        /**
         * 行程结束纬度
         */
        double endY = info.getEndY();

        /**
         * 行程结束地点
         */
        String endAddress = defaultString(info.getEndAddress(), "");

        /**
         * 是否失效
         */
        boolean isValidate = info.getIsValidate();

        /**
         * 创建时间
         */
        Date createTime = info.getCreateTime();

        /**
         * 结束时间 [结束时候,存在有意义值]
         */
        Date endTime = info.getEndTime();

        /**
         * 用户请求的开始地址
         * [请求以及后流程时候,存在有意义值]
         */
        String requestStartAddress = defaultString(info.getRequestStartAddress(), "");

        /**
         * 用户请求开始的x,经度
         * [请求以及后流程时候,存在有意义值]
         */
        double requestStartX = info.getRequestStartX();

        /**
         * 用户请求开始的y,纬度
         * [请求以及后流程时候,存在有意义值]
         */
        double requestStartY = info.getRequestStartY();

        /**
         * 用户请求的结束地址
         * [结束时候,存在有意义值]
         */
        String requestEndAddress = defaultString(info.getRequestEndAddress(), "");

        /**
         * 用户请求结束的x,经度
         * [结束时候,存在有意义值]
         */
        double requestEndX = info.getRequestEndX();

        /**
         * 用户请求结束的y,纬度
         * [结束时候,存在有意义值]
         */
        double requestEndY = info.getRequestEndY();
        /**
         * 请求人名称
         */
        String userName = info.getUserName();

        /**
         * 请求人手机
         */
        String userPhone = info.getUserPhone();

        /**
         * 乘车人名称
         */
        String passengerName = info.getPassengerName();

        /**
         * 乘车人手机号码
         */
        String passengerPhone = info.getPassengerPhone();
        /**
         * 预约时间,若当前是预约类别,则当前预约时间不能空
         */
        long bespeakTime = 0;
        Date bespeakDate = info.getBespeakTime();
        if (bespeakDate != null) {
            bespeakTime = bespeakDate.getTime();
        }

        view.setDistance(distance);
        view.setAcceptX(acceptX);
        view.setAcceptY(acceptY);
        view.setStartX(startX);
        view.setStartY(startY);
        view.setStartAddress(startAddress);
        view.setEndX(endX);
        view.setEndY(endY);
        view.setEndAddress(endAddress);
        view.setValidate(isValidate);
        view.setCreateTime(createTime);
        view.setEndTime(endTime);
        view.setRequestStartAddress(requestStartAddress);
        view.setRequestStartX(requestStartX);
        view.setRequestStartY(requestStartY);
        view.setRequestEndAddress(requestEndAddress);
        view.setRequestEndX(requestEndX);
        view.setRequestEndY(requestEndY);
        view.setUserName(userName);
        view.setUserPhone(userPhone);
        view.setPassengerName(passengerName);
        view.setPassengerPhone(passengerPhone);
        view.setBespeakTime(bespeakTime);
        return;
    }

    /**
     * @param businessService 服务信息公共信息
     * @param view            当前视图信息
     * @author Anson
     * @date 17-5-16
     * @name 封装视图公共信息
     * @since 1.0.4
     */
    public static void packageServiceView(BusinessService businessService, BusinessView view) {
        if (businessService == null || view == null) {
            return;
        }
        /** 是否已经支付了 与订单状态一直 */
        int orderStatus = businessService.getOrderStatus();
        /**当前订单id 且是1:1的关系  [不一定存在有效值,要根据实际业务在状态情况]*/
        String orderId = defaultString(businessService.getOrderId(), "");
        /** 业务流水号 且是1:1的关系 */
        String businessId = defaultString(businessService.getBusinessId(), "");
        /** 请求用户的id*/
        String userId = defaultString(businessService.getUserId(), "");
        /**司机id*/
        String driverId = defaultString(businessService.getDriverId(), "");
        /**状态 ,20:开始;30:进行;40:完毕;50：取消;60:下线;-10:异常*/
        int status = businessService.getStatus();
        //处理状态信息
        BusinessStatus statusEnum = BusinessStatus.getStatus(status);
        /**状态描述*/
        String statusMsg = statusEnum == null ? "" : statusEnum.getMsg();
        /**业务服务号*/
        int businessCode = businessService.getBusinessCode();
        /**服务费费率*/
        double serviceFee = businessService.getServiceFee();
        /**
         * 用户请求类型
         * 1.及时请求派车
         * 2.预约请求派车
         */
        int requestType = businessService.getRequestType();
        /**
         * 是否自己用车
         */
        Boolean isRideSelf = businessService.getRideSelf();
        view.setOrderStatus(orderStatus);
        view.setOrderId(orderId);
        view.setBusinessId(businessId);
        view.setUserId(userId);
        view.setDriverId(driverId);
        view.setStatus(status);
        view.setStatusMsg(statusMsg);
        view.setBusinessCode(businessCode);
        view.setServiceFee(serviceFee);
        view.setIsRideSelf(isRideSelf);
        view.setRequestType(requestType);
    }
}
